/*
 * MPCLinearPrograms.h
 *
 *  Created on: Sep 11, 2013
 *      Author: sjelic
 */

#ifndef MPCLINEARPROGRAMS_H_
#define MPCLINEARPROGRAMS_H_

#include<vector>
#include<fstream>

//#include "coveringPackingLP.h"
//#include "coveringPackingMiscellaneous.h"
//texture<matrix_type, 2, cudaReadModeElementType> tex;
#define real double
#define matrix_type float
const real INFTY = 1e200;

namespace mpclp
{
	class MPCLinearProgram
	{
		public:

			int objective; 	//objective =  1 minimization
							//objective = -1 maximization
			unsigned int num_var;
			unsigned int num_constr_pack; //number of packing constraints
			unsigned int num_constr_cov; //number of covering constraints
			unsigned long num_nonzero_elements_pack;
			unsigned long num_nonzero_elements_cov;

			unsigned int num_var_reduced; // number of packing variables
			unsigned int num_constr_pack_reduced; //number of packing constraints
			unsigned int num_constr_cov_reduced; //number of covering constraints after reduction
			unsigned int num_nonzero_elements_pack_reduced; //number of nonzero elements after reduction
			unsigned int num_nonzero_elements_cov_reduced; //number of nonzero elements after reduction

			matrix_type *constr_matr_P; // pointer to nonnegative packing constraint matrix P on CPU side
			matrix_type *constr_matr_C; // pointer to nonnegative covering constraint matrix C on CPU side
			real *constr_right_p;//Px<=p
			real *constr_right_c;//Cx>=c
			real *price_vec;//max w.x
			std::vector<unsigned int> constr_right_p_in;
			std::vector<unsigned int> constr_right_c_in;
			std::vector<unsigned int> price_vec_in;
			real *opt_var; //vector of optimal solution
			int status; // status=0 --> loaded
						// status=1 --> preprocessed
						// status=2 --> normalized
						// status=3 --> solved
			MPCLinearProgram(std::string);
			void readFromFile(std::string, int &, matrix_type**, matrix_type**, real**, real **, real **, unsigned int &, unsigned int &, unsigned int &, unsigned long &, unsigned long &);
			bool purePackingPreproccessModel(); /*true - feasible, false - infeasible*/
			bool pureCoveringPreproccessModel(); /*true - feasible, false - infeasible*/
	};

	class PackingLinearProgram
	{
	public:
			unsigned int num_constr_pack; //number of packing constraints
			unsigned int num_var_pack; // number of packing variables
			unsigned long num_nonzero_elements_pack;
			unsigned int num_constr_pack_reduced; //number of packing constraints
			unsigned int num_var_pack_reduced; // number of packing variables
			unsigned int num_nonzero_elements_pack_reduced; //number of nonzero elements after reduction
			matrix_type *constr_matr_P; // pointer to nonnegative constraint matrix on CPU side
			real *constr_right_p;//Px<=p
			std::vector<unsigned int> constr_right_p_in;
			real *price_vec;//max w.x
			std::vector<unsigned int> price_vec_in;
			real *opt_var; //vector of optimal solution
			int status; // status=0 --> loaded
						// status=1 --> preprocessed
						// status=2 --> normalized
						// status=3 --> solved

			PackingLinearProgram(std::string);
			void readFromFile(std::string, matrix_type**, real **, real **, unsigned int &, unsigned int &, unsigned long &);

			void normalizeModel();

			real densityInPercent();
			real optimalValue();



			~PackingLinearProgram();

	};

	class CoveringLinearProgram
	{
		public:
			unsigned int num_constr_cov; //number of covering constraints
			unsigned int num_var_cov; // number of covering variables
			unsigned long num_nonzero_elements_cov;
			unsigned int num_constr_cov_reduced; //number of covering constraints after reduction
			unsigned int num_var_cov_reduced; // number of covering variables after reduction
			unsigned int num_nonzero_elements_pack_reduced; //number of nonzero elements after reduction
			matrix_type *constr_matr_C; // pointer to nonnegative constraint matrix on CPU side
			real *constr_right_c;//Cx>=c
			std::vector<unsigned int> constr_right_c_in;
			real *price_vec;//min w.x
			std::vector<unsigned int> price_vec_in;
			real *opt_var; //vector of optimal solution
			int status; // status=0 --> loaded
						// status=1 --> pre-processed
						// status=2 --> normalized
						// status=3 --> solved
			CoveringLinearProgram(std::string);
			void readFromFile(std::string, matrix_type**, real **, real **, unsigned int &, unsigned int &, unsigned long &);
			bool preproccessModel(); /*true - feasible, false - infeasible*/
			void normalizeModel();

			real densityInPercent();
			real optimalValue();
	};







}


#endif /* MPCLINEARPROGRAMS_H_ */
